package net.realsee.service.wcs.device.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.device.WcsPlcChannel;
import net.realsee.domain.wcs.device.bo.WcsPlcChannelBo;
import net.realsee.domain.wcs.device.vo.WcsPlcChannelVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.device.WcsPlcChannelMapper;
import net.realsee.service.wcs.device.IWcsPlcChannelService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 通道Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsPlcChannelServiceImpl extends ServiceImpl<WcsPlcChannelMapper, WcsPlcChannel> implements IWcsPlcChannelService {

    private final WcsPlcChannelMapper baseMapper;

    /**
     * 查询通道
     */
    @Override
    public WcsPlcChannelVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询通道列表
     */
    @Override
    public TableDataInfo<WcsPlcChannelVo> selectVoPage(WcsPlcChannelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsPlcChannel> lqw = buildVoQueryWrapper(bo);
        Page<WcsPlcChannelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询通道列表
     */
    @Override
    public List<WcsPlcChannelVo> selectVoList(WcsPlcChannelBo bo) {
        LambdaQueryWrapper<WcsPlcChannel> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsPlcChannel> buildVoQueryWrapper(WcsPlcChannelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsPlcChannel> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsPlcChannel::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getChannel()), WcsPlcChannel::getChannel, bo.getChannel());
        lqw.like(StringUtils.isNotBlank(bo.getChannelName()), WcsPlcChannel::getChannelName, bo.getChannelName());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsPlcChannel::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsPlcChannel::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsPlcChannel::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsPlcChannel::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsPlcChannel::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsPlcChannel::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsPlcChannel::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsPlcChannel::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsPlcChannel::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsPlcChannel::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsPlcChannel::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsPlcChannel::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsPlcChannel::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增通道
     */
    @Override
    public Boolean insertByBo(WcsPlcChannelBo bo) {
        WcsPlcChannel add = BeanUtil.toBean(bo, WcsPlcChannel.class);
        return this.save(add);
    }

    /**
     * 修改通道
     */
    @Override
    public Boolean updateByBo(WcsPlcChannelBo bo) {
        WcsPlcChannel update = BeanUtil.toBean(bo, WcsPlcChannel.class);
        return this.updateById(update);
    }

    /**
     * 批量删除通道
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
